package com.vol.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vol.entity.SysDictionary;
import com.vol.entity.SysDictionarylist;
import com.vol.mapper.SysDictionaryMapper;
import com.vol.mapper.SysDictionarylistMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictionaryService {
    @Autowired
    private SysDictionaryMapper sysDictionaryMapper;
    @Autowired
    private SysDictionarylistMapper sysDictionarylistMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Map<String, Object>> GetVueDictionary(ArrayList<String> dicNameList) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (String dicName : dicNameList) {
            LambdaQueryWrapper<SysDictionary> qw = new LambdaQueryWrapper<>();
            qw.eq(SysDictionary::getDicno, dicName);
            SysDictionary sysDictionary = sysDictionaryMapper.selectOne(qw);
            if (sysDictionary != null) {
                Map<String, Object> map = new HashMap<>();
                map.put("dicNo", sysDictionary.getDicno());
                map.put("config", sysDictionary.getConfig());
                //此时要去查询sql语句
                if (sysDictionary.getDbsql() != null && !sysDictionary.getDbsql().isEmpty()) {
                    List<Map<String, Object>> data1 = jdbcTemplate.queryForList(sysDictionary.getDbsql());
                    map.put("data", data1);
                } else {//此时要去查询SysDictionarylist
                    LambdaQueryWrapper<SysDictionarylist> QWlist = new LambdaQueryWrapper<>();
                    QWlist.eq(SysDictionarylist::getDicId, sysDictionary.getDicId());
                    List<SysDictionarylist> list2 = sysDictionarylistMapper.selectList(QWlist);
                    List<Map<String, Object>> mapInnerList = new ArrayList<>();
                    for (SysDictionarylist sysDictionarylist : list2) {
                        Map<String, Object> mapInner = new HashMap<>();
                        mapInner.put("key", sysDictionarylist.getDicvalue());
                        mapInner.put("value", sysDictionarylist.getDicname());
                        mapInner.put("id", sysDictionarylist.getDiclistId());
                        mapInner.put("parentId", sysDictionarylist.getDicId());
                        mapInnerList.add(mapInner);
                    }
                    map.put("data", mapInnerList);
                }
                list.add(map);
            }
        }
        return list;
    }
}
